package com.mashibing.serviceverificationcode.controller;

import com.alibaba.fastjson.JSONObject;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.dto.serviceverificationcode.request.VerifyCodeRequest;
import com.mashibing.internalcommon.dto.serviceverificationcode.response.VerifyCodeResponse;
import com.mashibing.serviceverificationcode.service.VerifyCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author gorkr
 * @date 2022/04/27 18:10
 **/

@RestController
@RequestMapping("/verify-code")
@Slf4j
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;


    // 字符串运算效率 远低于数字运算。 所以生成验证码，采用数字运算。

    /**
     * 根据身份，手机号，生成验证码
     * @param identity
     * @param phoneNumber
     * @return
     */
    @GetMapping("/generate/{identity}/{phoneNumber}")
    public ResponseResult<VerifyCodeResponse> generate(@PathVariable("identity") int identity, @PathVariable("phoneNumber") String phoneNumber){
        log.info("/generate/{identity}/{phoneNumber}: 身份类型:{},手机号:{}",identity,phoneNumber);
        // 校验参数


        return verifyCodeService.generate(identity,phoneNumber);
    }

    @PostMapping("/verify")
    public ResponseResult verify(@RequestBody VerifyCodeRequest request){

        log.info("/verify-code/verify request:" + JSONObject.toJSON(request));
        // 获取手机号和验证码
        String phoneNumber = request.getPhoneNumber();
        int identity = request.getIdentity();
        String code = request.getCode();
        return verifyCodeService.verify(identity,phoneNumber,code);

    }

}
    